package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.MyMessageRelation;
import cn.wolfcode.trip.base.domain.Sms;
import cn.wolfcode.trip.base.mapper.MyMessageRelationMapper;
import cn.wolfcode.trip.base.mapper.SmsMapper;
import cn.wolfcode.trip.base.service.ISmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SmsServiceImpl implements ISmsService {

    @Autowired
    private SmsMapper smsMapper;
    @Autowired
    private MyMessageRelationMapper myMessageRelationMapper;

    @Override
    public void save(Sms sms) {
        //避免用户没有传值而导致报错
        if(sms.getImg()==null){
            sms.setImg("");
        }
        smsMapper.insert(sms);
        //在新增私信的同时要往我的信息的表中添加数据
        MyMessageRelation mmr = new MyMessageRelation();
        mmr.setCurrentuser(sms.getCurrentuser());
        mmr.setUser(sms.getUser());
        mmr.setMessagetype(MyMessageRelation.MESSAGETYPE_IN_SMS);
        myMessageRelationMapper.insertSmsUser(mmr);//新增信息记录
    }

    @Override
    public List<Sms> getSmsByUserId(Long id) {
        return smsMapper.selectForList(id);
    }

    @Override
    public void delete(Long id) {
        smsMapper.deleteByPrimaryKey(id);
    }
}
